<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            码垛机开关
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:30px;">
        </br>
        <div>
            <div class="aui-btn aui-btn-primary"
                style="width:40%;margin-left:30%;font-size: 20px;font-weight: bold;background-color: #03a9f4"
                onclick="changeSwitchStatus()" id="switch"></div>
        </div>
    </div>
    <div class="aui-content aui-margin-b-15" style="margin-top:10px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">标识位</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_Read_Bz" disabled>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">MES控制</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_ZD" disabled>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">总成去向</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_ZCQX" disabled>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">应码垛数量</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_YMDSL" disabled>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">垛唯一号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_DWYH" disabled>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">风险库道号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="MDJ_FXKDH">
                    </div>
                </div>
            </li>
        </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var interval;
    var intervalkg;
    var websocket;
    //初始化查询仓库下拉框
    apiready = function () {

        //查询开关状态
        searchSwitchStatus();

        //连接websocket
        initWebSocket();
    }

    function initWebSocket() {
        var netChoice = localStorage.getItem('netChoice');

        var serverIp = "";
        var serverPort = "";
        var serverProject = "";
        if (netChoice != null && netChoice == "in") {
            serverIp = localStorage.getItem('serverIpN');
            serverPort = localStorage.getItem('serverPortN');
            serverProject = localStorage.getItem('serverProjectN');
        } else {
            serverIp = localStorage.getItem('serverIpW');
            serverPort = localStorage.getItem('serverPortW');
            serverProject = localStorage.getItem('serverProjectW');
        }

        var websocket = new WebSocket('ws://' + serverIp + ':' + serverPort + '/' + serverProject + '/ws/stackerCrance');
        websocket.onopen = function () {
            console.log('WebSocket open');
        };
        websocket.onmessage = function (e) {
            var map = eval("(" + e.data + ")");
            setSingleValue(map);
        };
        websocket.onerror = function (e) {
            alert('WebSocket连接发生错误:' + e.data);
        };
        websocket.onclose = function (e) {
            alert('WebSocket连接关闭');
            websocket.close();
        };
    }

    //开关状态改变
    function changeSwitchStatus() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/changeSwitchStacker";
        var data = {
            values: {
                un: un,
                appVersion: api.appVersion,
                switchFlag: 'STACK_SWITCH',
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {

                    var switchStaus = myobj.switchStaus;
                    var div = document.getElementById("switch")
                    //关闭
                    if (switchStaus == "0") {
                        div.innerText = "开关:关";
                        div.style.backgroundColor = 'red';
                    } else if (switchStaus == "1") {
                        div.innerText = "开关:开";
                        div.style.backgroundColor = '#03a9f4';
                    }

                    //失败
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //查询开关状态
    function searchSwitchStatus() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getSwitchStacker";
        var data = {
            values: {
                un: un,
                appVersion: api.appVersion,
                switchFlag: 'STACK_SWITCH',
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功


                if (myobj.code == "0") {
                    var switchStaus = myobj.switchStaus;
                    var div = document.getElementById("switch")

                    //关闭
                    if (switchStaus == "0") {
                        div.innerText = "开关:关";
                        div.style.backgroundColor = 'red';
                    } else if (switchStaus == "1") {
                        div.innerText = "开关:开";
                        div.style.backgroundColor = '#03a9f4';
                    }

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //信号点赋值
    function setSingleValue(map) {
        for (var key in map) {
            $("#" + key).val(map[key]);
        }
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>